﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.DataAccess;
using SouthApps.MobileHealth.SDK.Common.Exceptions;
using SouthApps.MobileHealth.SDK.DAL.Model;
using SouthApps.MobileHealth.SDK.Common;

namespace SouthApps.MobileTasks.SDK.DAL.Repositories
{
    public class ConveniosRepository : IConveniosRepository
    {
        PlanDao planDao;
        EspecialidadDao especialidadDao;
        ConvenioDao convenioDao;

        public ConveniosRepository()
        {
            this.especialidadDao = new EspecialidadDao();
            this.planDao = new PlanDao();
            this.convenioDao = new ConvenioDao();
        }

        public void AddConvenio(Convenio convenio)
        {
            try
            {
                this.convenioDao.AddConvenio(convenio);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while adding a convenio.", ex.Message);
            }
        }

        public void AddEspecialidad(Especialidad especialidad)
        {
            try
            {
                this.especialidadDao.AddEspecialidad(especialidad);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while adding an especialidad.", ex.Message);
            }
        }

        public void AddPlan(Plan plan)
        {
            try
            {
                this.planDao.AddPlan(plan);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while adding a plan.", ex.Message);
            }
        }

        public List<Plan> GetPlanesFinanciador(int financiadorId)
        {
            try
            {
                return this.planDao.GetPlanes(financiadorId);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting planes financiador.", ex.Message);
            }
        }

        public List<Especialidad> GetEspecialidadesFinanciador(int financiadorId)
        {
            try
            {
                return this.especialidadDao.GetEspecialidades(financiadorId);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting especialidades financiador.", ex.Message);
            }
        }

        public List<Especialidad> GetEspecialidadesPlanFinanciador(int financiadorId, int planId)
        {
            try
            {
                return this.especialidadDao.GetEspecialidadesPlan(financiadorId, planId);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting especialidades plan financiador.", ex.Message);
            }
        }

        public List<Convenio> GetConveniosFinanciador(int financiadorId, int? planId, int? especialidadId, int? sucursalPrestadorId)
        {
            try
            {
                return this.convenioDao.GetConvenios(financiadorId, planId, especialidadId, sucursalPrestadorId);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting convenios financiador.", ex.Message);
            }
        }

        public void ModifyPlan(Plan plan)
        {
            try
            {
                this.planDao.ModifyPlan(plan);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while modifying a plan.", ex.Message);
            }
        }

        public void ModifyConvenio(Convenio convenio)
        {
            try
            {
                this.convenioDao.ModifyConvenio(convenio);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while modifying a convenio.", ex.Message);
            }
        }

        public void ModifyEspecialidad(Especialidad especialidad)
        {
            try
            {
                this.especialidadDao.ModifyEspecialidad(especialidad);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while modifying an especialidad.", ex.Message);
            }
        }
    }
}
